package com.atguigu.srb.core.service.impl;

import com.atguigu.srb.core.pojo.entity.Borrower;
import com.atguigu.srb.core.mapper.BorrowerMapper;
import com.atguigu.srb.core.pojo.entity.BorrowerAttach;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.pojo.entity.vo.BorrowerApprovalVO;
import com.atguigu.srb.core.pojo.entity.vo.BorrowerVO;
import com.atguigu.srb.core.pojo.entity.vo.UserInfoSearchVo;
import com.atguigu.srb.core.service.BorrowerAttachService;
import com.atguigu.srb.core.service.BorrowerService;
import com.atguigu.srb.core.service.DictService;
import com.atguigu.srb.core.service.UserInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 借款人 服务实现类
 * </p>
 *
 * @author Atguigu
 * @since 2023-03-25
 */
@Service
public class BorrowerServiceImpl extends ServiceImpl<BorrowerMapper, Borrower> implements BorrowerService {
    @Resource
    BorrowerAttachService borrowerAttachService;

    @Resource
    DictService dictService;

    @Resource
    UserInfoService userInfoService;

    @Override
    public void getListByKey(Page<Borrower> page, String key) {
        LambdaQueryWrapper<Borrower> wrapper = Wrappers.lambdaQuery(Borrower.class);
        if (StringUtils.isNotEmpty(key)){
            wrapper.like(Borrower::getName, key).or()
                    .like(Borrower::getMobile, key).or()
                    .like(Borrower::getIdCard, key);
        }
        this.page(page, wrapper);
    }

    @Override
    public BorrowerVO getBorrowerById(String id) {
        Borrower borrower = this.getById(id);
        List<BorrowerAttach> attaches = borrowerAttachService.list(Wrappers.lambdaQuery(BorrowerAttach.class)
                .eq(BorrowerAttach::getBorrowerId, id));
        //设置附件
        BorrowerVO borrowerVO = new BorrowerVO();
        borrowerVO.setBorrowerAttaches(attaches);
        BeanUtils.copyProperties(borrower, borrowerVO);
        //设置数据字典
        Integer returnSource = borrower.getReturnSource();
        Integer contactsRelation = borrower.getContactsRelation();
        Integer industry = borrower.getIndustry();
        Integer income = borrower.getIncome();
        Integer education = borrower.getEducation();
        //根据二级字典的值和一级字典的唯一标识：dictCode来查询自己的标题
        String returnSourceStr = dictService.getDictNameByCodeAndValue("returnSource", returnSource);
        String relationStr = dictService.getDictNameByCodeAndValue("relation", contactsRelation);
        String industryStr = dictService.getDictNameByCodeAndValue("industry", industry);
        String incomeStr = dictService.getDictNameByCodeAndValue("income", income);
        String educationStr = dictService.getDictNameByCodeAndValue("education", education);
        Map<String, String> map = new HashMap<>();
        map.put("returnSource", returnSourceStr);
        map.put("relation", relationStr);
        map.put("industry", industryStr);
        map.put("income", incomeStr);
        map.put("education", educationStr);
        borrowerVO.setDicts(map);
        return borrowerVO;
    }

    @Override
    public void approval(BorrowerApprovalVO vo) {
        //获取审批状态
        Integer status = vo.getStatus();
        //获取受审批人id
        Long borrowerId = vo.getBorrowerId();
        //获取审批对象
        Borrower borrower = this.getById(borrowerId);
        //更新审批对象的状态
        borrower.setStatus(status);
        this.updateById(borrower);
        //获取会员对象
        Long userId = borrower.getUserId();
        UserInfo userInfo = userInfoService.getById(userId);
        //更新会员的借款状态
        userInfo.setBorrowAuthStatus(status);
        if (status.intValue()==2){
            //说明认证通过
            Integer total = 0;
            if (vo.getInfoIntegral()!=null){
                total+=vo.getInfoIntegral();
            }
            if (vo.getIsIdCardOk()){
                total+=30;
            }
            if (vo.getIsCarOk()){
                total+=60;
            }
            if (vo.getIsHouseOk()){
                total+=90;
            }
            userInfo.setIntegral(total);
        }
        userInfoService.updateById(userInfo);
    }
}
